﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using BaseHelper.Text;

namespace DeviceSDK.LED.XianKe.XianKe
{
    public class PlayBill
    {

        private Encoding _encoding = Encoding.Default;
        private List<object> _ltItem = new List<object>();
       
        //[LIST]
        //ItemCount=002
        //Item00=2,1,0,1,1,\C000000\Fs32\T255000000000\B000000000000\U深圳显科科技有限公司
        //Item01=2,1,0,1,1,\C000000\Fs32\T000255000000\B000000000000\I000

        public List<object> Items
        {
            get 
            {
                return _ltItem;
            }
            set
            {
                _ltItem = value;
            }
        }


        public void Clear()
        {
            _ltItem.Clear();
        }

        public void RemoveLastItem()
        {
            if (_ltItem.Count > 0)
                _ltItem.RemoveAt(_ltItem.Count - 1);
        }

        public bool AddImage(string FileName,int PlayTime,int X,int Y)
        {
            if (!FileName.ToLower().Contains(".bmp"))
                return false;

            ImageItem img = new ImageItem();
            img.FilePath = FileName;
            img.PlayTime=PlayTime;
            img.X=X;
            img.Y=Y;
            _ltItem.Add(img);
            return true;
        }

        public bool AddSource(string Line)
        {
            SourceItem item = new SourceItem();
            item.Line = Line;
            _ltItem.Add(item);
            return true;
        }

        public void AddString(string Text, int PlayTime = 10, string FontName = "黑体", int FontSize = 32, string FontColor = "#00FF00", int X = 0, int Y = 0, int Width = 0, int Height = 0, int Hor=2,int Ver=2,string BackColor = "#000000", int EffectIn = 1)
        {
            StringItem item = new StringItem();

            item.X = X;
            item.Y = Y;
            item.Width = Width;
            item.Height = Height;
            item.FontName = FontNameFormat(FontName);
            //item.FontSize = FontSizeFormat(FontSize);
            item.FontColor = ColorFormat(FontColor);
            item.BackColor = ColorFormat(BackColor);
            item.EffectIn = EffectIn;
            item.PlayTime = PlayTime;
            item.Hor = Hor;
            item.Ver = Ver;


            //判断是否是F架，因为F架一般屏幕比1:2，长是宽2倍，这里判断小于比例3就是F架
            bool IsFMode = ((float)Width / (float)Height) <= 3.0;

            //如果是F架
            if (IsFMode)
            {
                //如果是F架的，不会再进行计算字体大小和x，y坐标。直接默认左上角了。
                if (FontSize <= 1)
                {
                    FontSize = TextFontSizeHelper.CalcFontSizeByM(Text, Width, Height);
                }

                item.FontSize = FontSize;
                item.Text = Text;
                item.X = 0;
                item.Y = 0;
                _ltItem.Add(item);
            }
            else
            {
               
                //如果是门架，则查询是否包含换行符\n,如果包含的换，自动分屏。
                //同时，计算以下屏幕的左上角位置

                Text = Text.Replace("\r\n", "\n");//, "\\N");//.Replace("\\", "\\\\").
                if (Text.Contains("\n"))
                {
                    string[] Ts = Text.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);

                    foreach (string T in Ts)
                    {
                        if (T.Trim().Length == 0)
                            continue;

                        int tmpFontSize = FontSize;
                        if (FontSize <= 1)
                        {
                            tmpFontSize = TextFontSizeHelper.CalcFontSizeByM(T,  Width,Height);
                        }

                        StringItem itemClone = (StringItem)item.Clone();
                        itemClone.Text = T;
                        itemClone.FontSize = tmpFontSize;
                        TextFormat(ref itemClone);
                        _ltItem.Add(itemClone);
                    }

                }
                else
                {
                    if (FontSize <= 1)
                    {
                        FontSize = TextFontSizeHelper.CalcFontSizeByM(Text, Width,Height);
                    }

                    item.FontSize = FontSize;
                    item.Text = Text;
                    TextFormat(ref item);
                    _ltItem.Add(item);
                }
            }
           
          
            
        }


        private void TextFormat(ref StringItem item)
        {

            int Remain = 0;
            
            foreach (char c in item.Text)
            {
                if ((int)c <= 127)
                    Remain += item.FontSize / 2;
                else
                    Remain += item.FontSize;
            }

            try
            {
                //停车诱导屏默认就是居中显示
                if (item.Hor == 0)
                {
                    item.X = 0;
                }
                else if (item.Hor == 1)
                {
                    item.X = item.Width - Remain;
                }
                else if (item.Hor == 2)
                {
                    item.X = (item.Width - Remain) / 2;
                }
            }
            catch { }

            try
            {
                if (item.Ver == 0)
                {
                    item.Y = 0;
                }
                else if (item.Ver == 1)
                {
                    item.Y = item.Height - item.FontSize;
                }
                else if (item.Ver == 2)
                {
                    item.Y = (item.Height - item.FontSize) / 2;
                }
            }
            catch { }
         
        }


        private string ColorFormat(string ColorHex)
        { 
            switch(ColorHex)
            {
                case "#00FF00": return "000255000000";
                case "#FF0000": return "255000000000";
                case "#FFFF00": return "255255000000";
                case "#000000": return "000000000000";
            }
            return "000255000000";
        }

        private string FontNameFormat(string Font)
        {
            switch (Font)
            {
                case "宋体": return "s";
                case "仿宋": return "f";
                case "楷体":  return "k";
                case "黑体": return "h";

            }
            return "h";
        }

        private int FontSizeFormat(int FontSize)
        {
            int[] fontSize = new int[] { 64,48,32,24,16 };
            for (int i = 0; i < fontSize.Length; i++)
            {
                if (FontSize >= fontSize[i])
                    return fontSize[i];
            }

            return FontSize ;
        }

        public string Create()
        {
            //if (File.Exists(FilePath))
            //    File.Delete(FilePath);

            string output = "[LIST]\r\n";
            output += "ItemCount=" +_ltItem.Count.ToString().PadLeft(3,'0') + "\r\n";
          
            for (int i = 0; i < _ltItem.Count; i++)
            {
            
                if (_ltItem[i] is StringItem)
                {
                    StringItem item = (StringItem)_ltItem[i];
                    output += string.Format("Item{0}={1},{2},0,1,1,\\C{3}{4}\\F{5}{6}\\T{7}\\B{8}\\U{9}\r\n"
                        ,i.ToString().PadLeft(2,'0')
                        ,item.PlayTime
                        ,item.EffectIn
                        ,item.X.ToString().PadLeft(3, '0')
                        ,item.Y.ToString().PadLeft(3, '0')
                        ,item.FontName
                        ,item.FontSize
                        ,item.FontColor
                        ,item.BackColor
                        , item.Text
                    );
                }
                else if(_ltItem[i] is ImageItem)
                {
                    ImageItem item = (ImageItem)_ltItem[i];
                    
                    output += string.Format("Item{0}={1},{2},0,1,1,\\C{3}{4}\\I{5}\r\n"
                        , i.ToString().PadLeft(2, '0')
                        , item.PlayTime
                        , item.EffectIn
                        , item.X.ToString().PadLeft(3, '0')
                        , item.Y.ToString().PadLeft(3, '0')
                        , item.FileID==null?"":item.FileID.PadLeft(3, '0')
                    );

                }
                else if (_ltItem[i] is SourceItem)
                { 
                    SourceItem item=(SourceItem)_ltItem[i];
                    output += string.Format("Item{0}={1}\r\n"
                        , i.ToString().PadLeft(2, '0')
                        , item.Line);
                }
                
            }

            return output;

        }

    }

    class ItemBase
    {
        public int X = 0;
        public int Y = 0;
        public int Width = 0;
        public int Height = 0;

        public int PlayTime = 5;    //0.1s 默认      
        public int Hor = 2;
        public int Ver = 2;
        public int EffectIn = 1;

        public object Clone()
        {
            return this.MemberwiseClone();
        }
    }


    
    class StringItem : ItemBase
    {
        public string FontName = "黑体";     
        public int FontSize = 32;
        public string FontColor = "#00FF00";
        public string BackColor = "#000000";
        public string Text = "";  
    }


    class ImageItem : ItemBase
    {
        public string FileID;
        public string FilePath;
    }

    class SourceItem : ItemBase
    {
        public string Line;
    }
    
}
